const app = getApp();
const log =require('../../log.js');
const util=require('../../util.js');
const tabService = require("../../libs/tabService");
Page({

    /**
     * 页面的初始数据
     */
    data: {
        qrcode:'',
        showposter:false,
        share:false,
        use:false,
        cityIndex:0,
        type:'use',
        withStatus:1,
        icons:'https://image.browin.net.cn/icons/',
        user:{'role':0}
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let that=this;
        console.log('------mine-------');
        console.log(options);
        console.log('-------mine------');
        var parent_id=options.parent_id;
        if(parent_id!=undefined){
            this.setData({
                parent_id:parent_id
              })
              wx.setStorageSync('parent_id',parent_id);
        }else{
            parent_id = wx.getStorageSync('parent_id');
        
        }
        var userInfo=wx.getStorageSync('userInfo');
        console.log(userInfo);
        var date=util.formatTime(new Date());
        log.info("Time:"+date+",page:mine,options:"+JSON.stringify(options));
        console.log(userInfo)
        log.info("Time:"+date+",page:mine,userInfo:"+JSON.stringify(userInfo));
        if(userInfo == ""){
            console.log('userInfo is null')
            wx.navigateTo({
              url: '../login/index?parent_id='+parent_id,
            })
        }else{
            console.log('--mine----')
            console.log(userInfo)
            console.log('----mine---')
        }
        wx.showShareMenu({
            menus: ['shareAppMessage'],
            success(res) {
              console.log(res)
            },
            fail(e) {
              console.log(e)
            }
          })
          wx.hideShareMenu({
              menus:['shareTimeline']
          })  
          let d =   app.globalData;  
        that.setData({
            platform:d.platform,    
            userInfo:userInfo,
             width:d.windowWidth,
            height:d.windowHeight,
            statusBarHeight:d.statusBarHeight,
            navHeight:d.navHeight,
            marginTop:d.navTitleTop,
            domain:d.domain,
            uploadUrl:d.uploadUrl
          })
    },
    goIndex(){
        wx.setStorageSync('store_id',0);
        
        wx.reLaunch({
          url: '../index/index',
        })
    },
    getIndex:function(){
        let that = this; 
        var userInfo=wx.getStorageSync('userInfo');
         var user_id=userInfo.id;
        // user_id=164255;      
        wx.request({
          url: app.globalData.domain+'mine/Index',
          data:{from:1,user_id:user_id},
          success:res=>{
              var data=res.data;
              console.log(data);
            that.setData({
             shareTitle:data.shareTitle,
             shareLogo:data.shareLogo,
             shareIndex:data.shareIndex,
             is_sign:data.is_sign,
              notice:data.index,
              user:data.user,
              coupon:data.coupon,
              
            })
          }
      })
    },
    Index:function(){
        let that = this; 
        var userInfo=wx.getStorageSync('userInfo');
         var user_id=userInfo.id;
       //  user_id=164255;  
               
        wx.request({
          url: app.globalData.domain+'mine/Index',
          data:{from:1,user_id:user_id},
          success:res=>{
              var data=res.data;
              console.log(data);
            that.setData({
             shareTitle:data.shareTitle,
             shareLogo:data.shareLogo,
             shareIndex:data.shareIndex,
             is_sign:data.is_sign,
              notice:data.index,
              user:data.user,
              staff:data.staff,
              coupon:data.coupon
            })
          }
      })
    },
    goto:function(e){
        console.log(e);
        var url=e.currentTarget.dataset.url;
        var index=e.currentTarget.dataset.index;
        var parent_id=wx.getStorageSync('parent_id');
        app.goto(url,index)
        console.log(url);
    },
    

    sign:function(){
        let that=this;
        var userInfo=wx.getStorageSync('userInfo'); 
        wx.request({
            url: app.globalData.domain+'home/sign',
            data:{from:1,user_id:userInfo.id},
            success:res=>{
                console.log(res);
                var data=res.data;
                if(data.status==200){
                    wx.showModal({
                        title: '系统提示',
                        content:data.message,
                        success:function(res){
                           // that.useClose();
                          //  that.getList();
                            that.setData({
                                is_sign:1
                            })
                        }
                    })
                }else{
                    wx.showModal({
                        title: '系统提示',
                        content:data.message,
                        success:function(res){
                            that.setData({
                                is_sign:1
                            })
                            return false;
                        }
                    })
                }
            }
        })
    },
    
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {    
       
        //this.getIndex();
        this.Index();
        var role = wx.getStorageSync('role');
       console.log(role);
       this.update(role);
        var tabIndex;
        if(role==0){
          tabIndex=1;
        }else if(role==1){
          tabIndex=3;
        }else{
          tabIndex=3;
        }
           
        tabService.updateIndex(this, tabIndex);
      },
      update(role) {
        tabService.updateRole(this, role);
      },

    createPoster(){
        var userInfo=wx.getStorageSync('userInfo');
    let that=this;
    var user_id=userInfo.id;
    if(user_id){
      wx.request({
        url: app.globalData.domain+'mine/poster',
      data:{user_id:user_id,from:3},
      success:function(res){
        console.log(res);
        var statusCode=res.statusCode;
        if(statusCode==200){
            var qrcodeUrl=app.globalData.uploadUrl+res.data.qrcode;
            console.log(qrcodeUrl);
            that.setData({
             showposter:true,
              qrcode:qrcodeUrl
            }) 
        }else{
            that.showModals('网络错误');
        }
        

      },
      fail:function(e){
        console.log(e);
      }
      })

    }
    },
    closePoster(){
        this.setData({
            showposter:false
        })
    },

    save () {
        let that = this
        //若二维码未加载完毕，加个动画提高用户体验
        wx.showToast({
         icon: 'loading',
         title: '正在保存图片',
         duration: 1000
        })
        //判断用户是否授权"保存到相册"
        wx.getSetting({
         success (res) {
          //没有权限，发起授权
          if (!res.authSetting['scope.writePhotosAlbum']) {
           wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success () {//用户允许授权，保存图片到相册
             that.savePhoto();
            },
            fail () {//用户点击拒绝授权，跳转到设置页，引导用户授权
             wx.openSetting({
              success () {
               wx.authorize({
                scope: 'scope.writePhotosAlbum',
                success() {
                 that.savePhoto();
                }
               })
              }
             })
            }
           })
          } else {//用户已授权，保存到相册
           that.savePhoto()
          }
         }
        })
       },
      //保存图片到相册，提示保存成功
       savePhoto() {
        let that = this
        var qrcode=this.data.qrcode;
        wx.downloadFile({
         url: qrcode,
         success: function (res) {
          wx.saveImageToPhotosAlbum({
           filePath: res.tempFilePath,
           success(res) {
            wx.showToast({
             title: '保存成功',
             icon: "success",
             duration: 1000
            })
           }
          })
         }
        })
       },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },
  
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },
   onPageScroll(e){
    //console.log(e)
    var scrollTop = e.scrollTop;
    var show = 0 ;
    if(scrollTop>=60){
       show=1;
    }
    this.setData({
        show:show
    })
   },
    /**
     * 用户点击右上角分享
     */
    setShare:function(id,md5){
        var userInfo=wx.getStorageSync('userInfo');
        var date=new Date();
        var time=date.getTime();       
        var user_id=userInfo.id;
        
        wx.request({
            url: app.globalData.domain+'share/index',
            data:{user_id:user_id,from:3,id:id,page:'mine',sn:md5},
            success:function(res){
                console.log(res);
            }
        }) 
   
    
        console.log('setShare');
        //var md5=;

    },
    /* 分享朋友圈 */
    onShareTimeline(){
        //console.log(e);
        var title=this.data.shareTitle;
        var shareLogo=this.data.shareLogo;
        var userInfo=wx.getStorageSync('userInfo');
        var parent_id=userInfo.id;
        var date=new Date();
        var time=date.getTime();
        var page='mine';
        var time=date.getTime();
        var second=date.getSeconds();
        var hour=date.getHours();  
        var day=date.getDate();  
        var md5=day+'M'+hour+'in'+second+'e';
        var store_id=wx.getStorageSync('store_id');
         this.setData({
           share:true
         });
         console.log(shareLogo);
         console.log(title);
         this.setShare(1,md5);
         return{
           title: title,                 
           query: 'store_id='+store_id+'&parent_id='+parent_id+'&sn='+md5,        
           imageUrl:shareLogo      
           }
       },
    hiddenShare:function(){
        this.setData({
          share:false
        })
      },
      /* 分享好友 */  
    onShareAppMessage(e) {
        var userInfo=wx.getStorageSync('userInfo');
        var store_id=wx.getStorageSync('store_id');
        var parent_id=userInfo.id;
        var that = this;
        var title=this.data.shareTitle;
        var shareLogo=this.data.shareIndex;
        var date=new Date();
        var time=date.getTime();
        var page='mine';
        var time=date.getTime();
        var second=date.getSeconds();
        var hour=date.getHours();    
        var day=date.getDate();  
        var md5=day+'M'+hour+'in'+second+'e';
   　　var shareObj = {
   　　　　title: title,    
   　　　　path: '/pages/index/index?store_id='+store_id+'&parent_id='+parent_id+'&sn='+md5,   
   　　　　imageUrl: shareLogo 
   　　　
   　　}

   　　// 来自页面内的按钮的转发
   　　if( e.from == 'button' ){
     　　　shareObj.title=title;
   　　　　shareObj.path = '/pages/index/index?store_id='+store_id+'&parent_id='+parent_id+'&sn='+md5;
   　　}
   this.setShare(2,md5);
   　　// 返回shareObj
   　　return shareObj;
    }
})